<template>
  <form class="xtx-form" @submit="onSubmit">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input
          class="input"
          type="text"
          placeholder="请输入用户名"
          v-model="accountField"
        />
      </div>
      <div class="error" v-if="accountError">{{ accountError }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          class="input"
          type="text"
          placeholder="请输入手机号"
          v-model="mobileField"
        />
      </div>
      <div class="error" v-if="mobileError">{{ mobileError }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          class="input"
          type="text"
          placeholder="请输入验证码"
          v-model="codeField"
        />
        <span class="code" @click="getMsgCode">{{
          isActive ? `剩余${count}秒` : "发送验证码"
        }}</span>
      </div>
      <div class="error" v-if="codeError">{{ codeError }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input
          class="input"
          type="password"
          placeholder="请输入密码"
          v-model="passwordField"
        />
      </div>
      <div class="error" v-if="passwordError">{{ passwordError }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input
          class="input"
          type="password"
          placeholder="请确认密码"
          v-model="rePasswordField"
        />
      </div>
      <div class="error" v-if="rePasswordError">{{ rePasswordError }}</div>
    </div>
    <input type="submit" class="submit" value="立即绑定" />
  </form>
</template>

<script>
import { useField, useForm } from "vee-validate";
import {
  checkUserAccount,
  code,
  mobile,
  password,
  rePassword,
} from "@/utils/vee-validate-schema";
import useCountDown from "@/hooks/useCountDown";
import Message from "@/components/library/Message";
import { getRegisterMsgCode, createNewAccountBindQQ } from "@/api/user";
import useLoginAfter from "@/hooks/useLoginAfter";
export default {
  name: "LoginCallbackBindPatch",
  props: {
    openId: {
      type: String,
    },
  },
  setup(props) {
    const { handleSubmit, getMobileIsValidate, ...test } =
      useBindNewAccountFormValid();
    // 获取倒计时值及开始方法
    const { count, start, isActive } = useCountDown();
    // 发送验证码
    const getMsgCode = async () => {
      const { isValid, mobile } = await getMobileIsValidate();
      if (isValid && !isActive.value) {
        try {
          // 获取验证码
          await getRegisterMsgCode(mobile);
          Message({ type: "success", text: "验证码发送成功" });
          return start(60);
        } catch (error) {
          Message({ type: "error", text: "验证码发送失败" });
        }
      }
    };
    const { loginSuccessful, loginFailed } = useLoginAfter();
    const onSubmit = handleSubmit((values) => {
      // 创建账号并绑定qq;
      createNewAccountBindQQ({
        unionId: props.openId,
        account: values.checkUserAccount,
        mobile: values.mobile,
        code: values.code,
        password: values.password,
      }).then(loginSuccessful, loginFailed);
    });
    return {
      onSubmit,
      ...test,
      getMsgCode,
      count,
      isActive,
    };
  },
};
// 验证表单信息
function useBindNewAccountFormValid() {
  const { handleSubmit } = useForm({
    validationSchema: {
      checkUserAccount,
      mobile,
      code,
      password,
      rePassword,
    },
  });
  // 创建用于验证用户名的表单项验证对象
  const { value: accountField, errorMessage: accountError } =
    useField("checkUserAccount");
  // 创建用户验证手机号的表单项验证对象
  const {
    value: mobileField,
    errorMessage: mobileError,
    validate: mobileValidate,
  } = useField("mobile");
  // 创建用于验证手机验证码格式的表单项验证对象
  const { value: codeField, errorMessage: codeError } = useField("code");
  // 创建用于验证密码的表单项验证对象
  const { value: passwordField, errorMessage: passwordError } =
    useField("password");
  // 创建用于验证两次密码是否输入一致的表单验证项对象
  const { value: rePasswordField, errorMessage: rePasswordError } =
    useField("rePassword");
  // 单独验证手机号
  const getMobileIsValidate = async () => {
    // 验证用户输入的手机号格式是否正确
    let { valid } = await mobileValidate();
    // 将验证结果和手机号返回出去
    return { isValid: valid, mobile: mobileField.value };
  };
  return {
    handleSubmit,
    accountField,
    accountError,
    mobileField,
    mobileError,
    codeField,
    codeError,
    passwordField,
    passwordError,
    rePasswordField,
    rePasswordError,
    getMobileIsValidate,
  };
}
</script>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
